﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using NFlickr.Core;

namespace WindowsFormsApplication1
{
    public class StudentQueryObject<T> : IQueryable<T>
    {
        private Expression _expression;

        public StudentQueryObject(Expression expression)
        {
            _expression = expression;
        }
        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            MethodCallExpression callExpression = (MethodCallExpression)_expression;
            PhotoList photoList = (PhotoList)(((ConstantExpression)callExpression.Arguments[0]).Value);

            UnaryExpression quote = (UnaryExpression)callExpression.Arguments[1];

            Expression<Func<Photo, T>> projector = (Expression<Func<Photo, T>>)quote.Operand;
            Func<Photo, T> func = projector.Compile();

            List<T> list = new List<T>();


            foreach (Photo photo in photoList)
            {
                list.Add(func(photo));
            }
            return list.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        #region IQueryable Members

        public Type ElementType
        {
            get { return typeof(T); }
        }

        public System.Linq.Expressions.Expression Expression
        {
            get { return _expression; }
        }

        public IQueryProvider Provider
        {
            get { return null; }
        }

        #endregion
    }
}
